package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/18 11:27
 */
public class C26 {

    // https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
    public static int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int slow = 0, fast = 1;
        while (fast < nums.length) {
            if (nums[fast] == nums[slow]) {
                fast++;
            } else {
                nums[++slow] = nums[fast++];
            }
        }
        return slow + 1;
    }

    public static int removeDuplicates2(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int slow = 0, fast = 0;
        do {
            if (nums[fast] != nums[slow])
                nums[++slow] = nums[fast];
        } while (fast++ < nums.length);
        return slow + 1;
    }


    // for test
    public static void main(String[] args) {

       /* int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        System.out.println(removeDuplicates(nums));
        System.out.println(Arrays.toString(nums));*/
    }

}
